//
//  OtherICostDTViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/11/27.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class OtherICostDTViewController: ICostDTViewController {
    
    
    override func viewDidLoad() {
        self.other = true
        super.viewDidLoad()
    }
    
    override var titlelabels: [String] {
        get{
            return ["综合计划","立项预算","转化比例","项目执行按合同","执行比例","项目执行发票","执行比例","项目执行按收付款","执行比例"]
        }
    }
    
    @objc override func tolist(btn: UIButton)->Void{
        
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{
            if let success = json.contents?["success"]?.bool {
                if success == true  {
                    if let data = json.contents?["data"]?.array {
                        source = data
                        typealias NameDatas = (String,Array<JSON>)
                        let names = source.map { (json) -> String in
                            return json.contents?["deptName"]?.rawString() ?? ""
                            }.filterDuplicates({$0})
                        var datas: [NameDatas] = []
                        for name in names {
                            datas.append((name,source.filter({ (json) -> Bool in
                                return json.contents?["deptName"]?.rawString() == name
                            })))
                        }
                        //名字又了
                        let widths: [CGFloat] = [2 * widthunit,2 * widthunit,2 * widthunit,3 * widthunit,2 * widthunit,3 * widthunit,2 * widthunit,3 * widthunit,2 * widthunit]
                        for i in  0 ..< datas.count {
                            let data = datas[i]
                            let namelabel = button(with: data.0)
                            namelabel.left = 0
                            namelabel.top = CGFloat(CGFloat(2) * heightunit * CGFloat(i))
                            namelabel.width = 3 * widthunit
                            namelabel.height = CGFloat(2) * heightunit
                            namelabel.tag = 1000 + i
                            namelabel.addTarget(self, action: #selector(tolist(btn:)), for: .touchUpInside)
                            leftscroll.addSubview(namelabel)
                            leftscroll.contentSize = CGSize.init(width: 3 * widthunit, height: namelabel.bottom)//左边的标签先加上
                            //开始加右边的标签
                            for _ in 0 ..< data.1.count {
                                let one = data.1[0]
                                let plan = String((one.contents?["planMoney"]?.int ?? 0) / 1000000).rmbformat.trimmingCharacters(in: CharacterSet.init(charactersIn: "¥"))
                                let budget = String((one.contents?["budget"]?.int ?? 0) / 1000000).rmbformat.trimmingCharacters(in: CharacterSet.init(charactersIn: "¥"))
                                let ratio1 = String(format: "%.2f", Double((one.contents?["budget"]?.int ?? 0)) / Double((one.contents?["planMoney"]?.int ?? 0)) * 100) + "%"
                                let projContract = String((one.contents?["projContract"]?.int ?? 0) / 1000000).rmbformat.trimmingCharacters(in: CharacterSet.init(charactersIn: "¥"))
                                let ratio2 =  String(format: "%.2f", Double((one.contents?["projContract"]?.int ?? 0)) / Double((one.contents?["planMoney"]?.int ?? 0)) * 100) + "%"
                                
                                let projInvoice = String((one.contents?["projInvoice"]?.int ?? 0) / 1000000).rmbformat.trimmingCharacters(in: CharacterSet.init(charactersIn: "¥"))
                                let ratio3 = String(format: "%.2f", Double((one.contents?["projInvoice"]?.int ?? 0)) / Double((one.contents?["planMoney"]?.int ?? 0)) * 100) + "%"
                                
                                let projPayment = String((one.contents?["projPayment"]?.int ?? 0) / 1000000).rmbformat.trimmingCharacters(in: CharacterSet.init(charactersIn: "¥"))
                                let ratio4 = String(format: "%.2f", Double((one.contents?["projPayment"]?.int ?? 0)) / Double((one.contents?["planMoney"]?.int ?? 0)) * 100) + "%"
                                
                                let texts: [String] = [plan,budget,ratio1,projContract,ratio2,projInvoice,ratio3,projPayment,ratio4]
                                for k in 0 ..< texts.count {
                                    let text = label(width: texts[k])
                                    text.width = widths[k]
                                    text.top = CGFloat(CGFloat(i) * CGFloat(2) * heightunit)
                                    var left: CGFloat = 0
                                    for h in 0 ..< k {
                                        left = left + (widths[h])
                                    }
                                    text.left = left
                                    text.height = CGFloat(2) * heightunit
                                    contentscroll.addSubview(text)
                                }
                            }
                        }
                        //详情
//                        sss topscroll.contentSize = CGSize.init(width: maxwidth - 50, height: 0)
                        contentscroll.contentSize = CGSize.init(width: topscroll.contentSize.width, height: leftscroll.contentSize.height)
                        //加线段
                        let topview = UIView()
                        topview.frame = CGRect.init(origin: CGPoint(x: 0,y: 0), size: CGSize(width: self.view.width,height: 1))
                        self.view.addSubview(topview)
                        //头上加一条线
                        
                        let view = UIView()
                        view.backgroundColor = UIColor.hex("1C90D1")
                        view.frame = CGRect.init(origin: CGPoint(x: 0,y: heightunit), size: CGSize(width: 100000,height: 1))
                        self.view.addSubview(view)
                        //heightunit像素处加一条线

                        let vertical = UIView()
                        vertical.backgroundColor = UIColor.hex("1C90D1")
                        vertical.frame = CGRect.init(origin: CGPoint(x: 3 * widthunit,y: 0), size: CGSize(width: 1,height: self.view.height))
                        self.view.addSubview(vertical)
                        //x heightunit像素处加一条线
                        
                        for i in 1 ..< datas.count {
                            let view = UIView()
                            view.backgroundColor = UIColor.hex("1C90D1")
                            view.frame = CGRect.init(origin: CGPoint(x: 0,y: CGFloat(2) * heightunit * CGFloat(i)), size: CGSize(width: 3 * widthunit,height: 1))
                            leftscroll.addSubview(view)
                        }
                        //给左边的滑动视图加线
                        
                        
                        let widths1: [CGFloat] = [2 * widthunit,2 * widthunit,2 * widthunit,3 * widthunit,2 * widthunit,3 * widthunit,2 * widthunit,3 * widthunit,2 * widthunit]
                        for i in 0 ..< 8 {
                            let view = UIView()
                            view.backgroundColor = UIColor.hex("1C90D1")
                            var totalx: CGFloat = 0.0
                            for j in 0 ... i {
                                totalx = totalx + widths1[j]
                            }
                            view.frame = CGRect.init(origin: CGPoint(x: totalx,y: 0), size: CGSize(width: 1,height: heightunit))
                            topscroll.addSubview(view)
                        }//给上面的滑动视图加线
                        
                        for i in 0 ..< 8 {
                            let vertical = UIView()
                            vertical.backgroundColor = UIColor.hex("1C90D1")
                            var totalx: CGFloat = 0.0
                            for j in 0 ... i {
                                totalx = totalx + widths1[j]
                            }
                            vertical.frame = CGRect.init(origin: CGPoint(x: totalx,y: 0), size: CGSize(width: 1,height: 10000))
                            contentscroll.addSubview(vertical)
                        }
                        //给content加竖线
                        for i in 1 ..< datas.count {
                            let horizontal = UIView()
                            horizontal.backgroundColor = UIColor.hex("1C90D1")
                            horizontal.frame = CGRect.init(origin: CGPoint(x: 0,y: CGFloat(2) * heightunit * CGFloat(i)), size: CGSize(width: 10000,height: 1))
                            contentscroll.addSubview(horizontal)
                        }//给content加横线
                    }
                }
            } else {
                alert(json.contents?["errorMsg"]?.rawString())
            }
        }
        return suc
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        topscroll.contentSize = CGSize.init(width: widthunit * 22 - 50, height: 0)
    }
}
